<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="产品名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="产品名称"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="价格"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
      </el-form-item>
       <el-form-item label="时间" prop="time">
        <el-input v-model="dataForm.time" placeholder="时间"></el-input>
      </el-form-item>
      <el-form-item label="发货地" prop="place">
        <el-input v-model="dataForm.place" placeholder="发货地"></el-input>
      </el-form-item>
      <!-- <el-form-item label="图片" prop="imageUrl">
        <el-input v-model="dataForm.imageUrl" placeholder="图片"></el-input>
      </el-form-item> -->
       <el-form-item   label="图片路径" prop="imageUrl">
        <el-upload
           v-model="dataForm.imageUrl"
          class="upload-demo"
          ref="upload"
          :http-request = "customUpload"
          drag
          action=""
          multiple
          >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">文件大小不超过50M</div>
        </el-upload>
      </el-form-item> 
      <el-form-item label="产品简介" prop="introduction">
        <el-input v-model="dataForm.introduction" placeholder="产品简介"></el-input>
      </el-form-item>
       <!-- <el-form-item label="二维码" prop="code">
        <el-input v-model="dataForm.code" placeholder="二维码"></el-input>
      </el-form-item> -->
      <el-form-item   label="二维码" prop="code">
        <el-upload
           v-model="dataForm.code"
          class="upload-demo"
          ref="upload"
          :http-request = "customUpload"
          drag
          action=""
          multiple
          >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">文件大小不超过50M</div>
        </el-upload>
      </el-form-item> 
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { fileUpload } from '@/utils';
//import QRCode from 'qrcode'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        price: "",
        phone: "",
        time: "",
        place: "",
        imageUrl: [],
        introduction: "",
        code:""
      },
      dataRule: {
        name: [{ required: true, message: "产品名称不能为空", trigger: "blur" }],
        price: [{ required: true, message: "价格不能为空", trigger: "blur" }],
       phone: [
          { required: true, message: "电话不能为空", trigger: "blur" }
        ],
        time: [{ required: true, message: "时间不能为空", trigger: "blur" }],
        place: [{ required: true, message: "发货地不能为空", trigger: "blur" }],
        introduction: [{ required: true, message: "产品简介不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
     customUpload(imageUrl){
        fileUpload(imageUrl).then(response => {
           this.dataForm.imageUrl.push(response.data.url) ;
          })
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/huinong/productSupply/delete`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.supply.name;
              this.dataForm.price = data.supply.price;
              this.dataForm.phone= data.supply.phone; 
              this.dataForm.time= data.supply.time; 
              this.dataForm.place = data.supply.place;
              this.dataForm.imageUrl = data.supply.imageUrl;
              this.dataForm.introduction = data.supply.introduction;
              this.dataForm.code=data.supply.code;

            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/huinong/productSupply/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              price: this.dataForm. price,
              phone: this.dataForm.phone, 
              time: this.dataForm.time, 
              place:this.dataForm.place,
              imageUrl:this.dataForm.imageUrl.join(),
              introduction:this.dataForm.introduction,
              code:this.dataForm.code,
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
